import { defineEventHandler, readBody } from "h3"
import { UserService } from "../../../services/userService"
import { UserPasswordChangeSchema } from '@/schema/user.schema'

export default defineEventHandler(async (event) => {
  const body = await readBody(event)
  // 获取请求体数据
  const result = UserPasswordChangeSchema().safeParse(body)
  if (!result.success) {
    // 校验失败，返回错误信息
    throw createError({
      statusCode: 400,
      statusMessage: result.error.name,
      data: result.error.issues,
    })
  }
  const { currentPassword, newPassword, confirmPassword } = body
  
  // 获取用户ID（从认证中间件获取）
  const userId = event.context.session?.id
  // 调用修改密码服务
  await UserService.changePassword(userId, currentPassword, newPassword, confirmPassword)
  
  // 返回成功响应
  return {
    statusCode: 200,
    statusMessage: "password.updateSuccess"
  }
})
